package org.cytoscape.myapp.cytoscape_app_v1.internal;


import static org.cytoscape.work.ServiceProperties.ID;
import static org.cytoscape.work.ServiceProperties.MENU_GRAVITY;
import static org.cytoscape.work.ServiceProperties.PREFERRED_MENU;
import static org.cytoscape.work.ServiceProperties.TITLE;
import static org.cytoscape.work.ServiceProperties.ENABLE_FOR;


import java.util.Properties;

import org.cytoscape.application.CyApplicationManager;
import org.cytoscape.application.events.SetCurrentNetworkListener;
import org.cytoscape.application.swing.CyNodeViewContextMenuFactory;
import org.cytoscape.application.swing.CytoPanelComponent;
import org.cytoscape.event.CyEventHelper;
import org.cytoscape.group.CyGroupFactory;
import org.cytoscape.group.CyGroupManager;
import org.cytoscape.model.CyNetworkManager;
import org.cytoscape.model.subnetwork.CyRootNetworkManager;
import org.cytoscape.myapp.cytoscape_app_v1.internal.UI.CytoPanel;
import org.cytoscape.myapp.cytoscape_app_v1.internal.UI.RightClickMenu;
import org.cytoscape.myapp.cytoscape_app_v1.internal.UI.ViewUtilListener;
import org.cytoscape.myapp.cytoscape_app_v1.internal.layouts.CyLayoutsImpl;
import org.cytoscape.myapp.cytoscape_app_v1.internal.model.NetworkManager;
import org.cytoscape.myapp.cytoscape_app_v1.internal.model.UtilNetworkManager;
import org.cytoscape.myapp.cytoscape_app_v1.internal.tasks.CreateSubnetworkTask;
import org.cytoscape.myapp.cytoscape_app_v1.internal.visual.VisualInstanceDependecies;
import org.cytoscape.property.CyProperty;
import org.cytoscape.service.util.AbstractCyActivator;
import org.cytoscape.service.util.CyServiceRegistrar;
import org.cytoscape.view.layout.CyLayoutAlgorithm;
import org.cytoscape.view.layout.CyLayoutAlgorithmManager;
import org.cytoscape.view.model.CyNetworkViewFactory;
import org.cytoscape.view.model.CyNetworkViewManager;
import org.cytoscape.view.model.events.NetworkViewAddedListener;
import org.cytoscape.view.vizmap.VisualMappingFunctionFactory;
import org.cytoscape.view.vizmap.VisualMappingManager;
import org.cytoscape.view.vizmap.VisualStyleFactory;
import org.cytoscape.work.TaskManager;
import org.cytoscape.work.undo.UndoSupport;
import org.osgi.framework.BundleContext;

public class CyActivator extends AbstractCyActivator {
	
	protected CyApplicationManager applicationManager;
	
	protected CyNetworkManager networkManager;
	
	protected CyGroupManager groupManager;
	
	protected CyGroupFactory groupFactory;
	
	protected CyNetworkViewManager networkViewManager;
	
	protected CyNetworkViewFactory networkViewFactory;
	
	protected TaskManager taskManager;
	
	protected CyEventHelper eventHelper;
	
	protected CyRootNetworkManager rootNetwork;
	
	protected CyLayoutAlgorithmManager layoutAlgorithmManager;
	
	protected CyLayoutsImpl cyLayouts;
	
	protected UndoSupport undoSupportServiceRef;
	
	protected CyProperty cyPropertyServiceRef;
	
	protected VisualInstanceDependecies visualInstanceDependecies;
	
	protected UtilNetworkManager utilNetworkManager;
	
	protected CytoPanel panel;

	@Override
	public void start(BundleContext context) throws Exception {
		
		
		this.addCommonResources(context);
        
		this.addListeners(context);
		
		this.adddRightClickMenu(context);		    
	  
		this.addStyleResources(context);
		
		this.addLayoutResources(context);
		
		utilNetworkManager = new NetworkManager(applicationManager, groupManager, groupFactory, visualInstanceDependecies, cyLayouts, undoSupportServiceRef);
		
		this.panel =  new CytoPanel(applicationManager,groupManager,taskManager, utilNetworkManager, eventHelper);

		registerService(context, panel, CytoPanelComponent.class, new Properties());
		registerService(context, panel, SetCurrentNetworkListener.class, new Properties());
	}
	
	private void addCommonResources(BundleContext context)
	{
		this.applicationManager = getService(context, CyApplicationManager.class);
        
        this.networkManager = getService(context, CyNetworkManager.class);
        
        this.groupManager = getService(context, CyGroupManager.class);
        
        this.groupFactory = getService(context, CyGroupFactory.class);
        
         this.networkViewManager = getService(context, CyNetworkViewManager.class);
        
        this.networkViewFactory = getService(context, CyNetworkViewFactory.class);
		
        this.taskManager = getService(context, TaskManager.class);
        
        this.eventHelper = getService(context, CyEventHelper.class);
        
        this.rootNetwork = getService(context,CyRootNetworkManager.class);
        
        this.layoutAlgorithmManager = getService(context, CyLayoutAlgorithmManager.class); 
	}
	
	private void adddRightClickMenu(BundleContext context)
	{
		CreateSubnetworkTask createSub = new CreateSubnetworkTask(applicationManager, groupManager, networkManager,
					networkViewManager, networkViewFactory, rootNetwork);

		Properties settingsProps = new Properties();
		settingsProps.setProperty(ID,"RightClickMenu");
		settingsProps.setProperty(PREFERRED_MENU,"SUB-RED");
		settingsProps.setProperty(MENU_GRAVITY, "-1");
		settingsProps.setProperty(TITLE, "GENERAR SUB-RED");
		settingsProps.setProperty(ENABLE_FOR,"false");
		
		
		RightClickMenu click= new RightClickMenu(settingsProps,createSub,taskManager,groupManager);
		registerService(context,click,CyNodeViewContextMenuFactory.class, settingsProps);
		
	}
	
	private void addListeners(BundleContext context)
	{
		ViewUtilListener viewDefaultListener = new ViewUtilListener(layoutAlgorithmManager, taskManager);
		
		registerService(context, viewDefaultListener, NetworkViewAddedListener.class, new Properties());
	}
	
	private void addLayoutResources(BundleContext context)
	{
		this.cyPropertyServiceRef = getService(context,CyProperty.class,"(cyPropertyName=cytoscape3.props)");
		
		this.undoSupportServiceRef = getService(context,UndoSupport.class);
		
		CyServiceRegistrar cyServiceRegistrar = getService(context,CyServiceRegistrar.class);
		
		this.cyLayouts = new CyLayoutsImpl(cyServiceRegistrar, cyPropertyServiceRef, null);
		
		registerService(context,cyLayouts,CyLayoutAlgorithmManager.class, new Properties());

		registerServiceListener(context,cyLayouts,"addLayout","removeLayout",CyLayoutAlgorithm.class);
	}
	
	private void addStyleResources(BundleContext context)
	{	
		VisualMappingManager vmmServiceRef = getService(context,VisualMappingManager.class);
		
		VisualStyleFactory visualStyleFactoryServiceRef = getService(context,VisualStyleFactory.class);
		
		VisualMappingFunctionFactory vmfFactoryC = getService(context,VisualMappingFunctionFactory.class, "(mapping.type=continuous)");
		VisualMappingFunctionFactory vmfFactoryD = getService(context,VisualMappingFunctionFactory.class, "(mapping.type=discrete)");
		VisualMappingFunctionFactory vmfFactoryP = getService(context,VisualMappingFunctionFactory.class, "(mapping.type=passthrough)");
		
		this.visualInstanceDependecies = new VisualInstanceDependecies(vmmServiceRef, visualStyleFactoryServiceRef, vmfFactoryC, vmfFactoryD, vmfFactoryP, applicationManager);
	}
	

}
